package com.jwt.demo.util;

import com.jwt.demo.db.mapper.UserLoginMapper;
import org.springframework.beans.factory.annotation.Autowired;
import java.time.LocalDate;
import java.util.Random;

/**
 * 数字工具类
 * @author chenlirun
 * @date 2021/7/8 15:55
 */
public class RandomUtil {

    @Autowired
    private static UserLoginMapper userLoginMapper;

    /**
     * 生成随机盐
     * @author chenlirun
     * @date 2021/7/8 15:56
     */
    public static String getSalt(){
        String str="zxcvbnmasdfghjklqwertyuiopZXCVBNMASDFGHJKLQWERTYUIOP1234567890,.<>:?";
        Random random = new Random();
        StringBuffer stringBuffer = new StringBuffer();
        //循环16次，共取出16个随机字符
        for (int i = 0; i < 16; i++) {
            //每次生成一个67以内的随机数
            int number = random.nextInt(68);
            //生成的随机数作为 str 字符串的下标；从 str 中取出随机字符后追加到 stringBuffer
            stringBuffer.append(str.charAt(number));
        }
        return stringBuffer.toString();
    }

    /**
     * 生成账号
     * @author chenlirun
     * @date 2021/7/14 9:28
     */
    public static Long getNumber(){
        LocalDate localDate = LocalDate.now();
        int year = localDate.getYear();
        StringBuffer stringBuffer = new StringBuffer();
        Random random = new Random();
        for (int i = 0; i < 8; i++) {
            int num = random.nextInt(9);
            stringBuffer.append(num);
        }
        String number =String.valueOf(year)+stringBuffer;
        return Long.parseLong(number);
    }

//    /**
//     * 账号生成规则
//     * @author chenlirun
//     * @date 2021/7/14 9:43
//     */
//    public static int isNumber(Integer number){
//        if (number==null){
//            throw new BaseException(BaseCodeResult.ERROR,"生成账号发生错误，请联系管理员");
//        }
//        UserLogin userLogin = userLoginMapper.selectUserLoginByNumber(number);
//        if(ObjectUtil.isNotEmpty(userLogin)){
//            return RandomUtil.getNumber();
//        }
//    }


}
